package com.hhq.demo.wechat.filter;

import com.hhq.demo.bean.User;
import com.hhq.demo.utils.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author HHQ
 * @date 2021年12月18日
 */
@WebFilter({"/wechat/index.html"})
public class WxLoginFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        User wxUserInfo = UserUtil.getWxUserInfo(req.getSession());
        resp.setContentType("text/html;charset=UTF-8");
        if(wxUserInfo != null) {
            filterChain.doFilter(req, resp);
        } else {
            resp.sendRedirect("/wechat/login.html");
        }
    }

    @Override
    public void destroy() {

    }
}
